#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "PluginInterface.h"
#include <QDir>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    if(loader.isLoaded())
    {
        loader.unload();
    }
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    QDir dir("F:/QT_code/1study/tetsPlugIn/Debug/debug");
    //QDir dir(qApp->applicationDirPath());//加载当前运行目录
    qDebug("目录1:%s",dir.path().toUtf8().constData());
    loader.setFileName(dir.filePath("PluginDemo.dll"));//将加载器的目录加载进来
    qDebug("目录2:%s",dir.filePath("PluginDemo.dll").toUtf8().constData());
    if(!loader.load())
    {
        qDebug("Failed to load loader");
        QMessageBox::critical(this,"Tips",loader.errorString());
        return ;
    }
    //实例化
    QObject *plugin = loader.instance();
    qDebug() << __FUNCTION__ << loader.errorString();
    if(plugin==NULL)
    {
        qDebug("loader.instance() is null");
        return;
    }
    qDebug("Success1");
    PluginInterface *pi = qobject_cast<PluginInterface *>(plugin);
    if(pi)
    {
        QWidget * w = pi->getWidget();
        qDebug("Success2");
        if(w)
        {
            ui->tabWidget->addTab(w,"hello plugin");
            qDebug("Success3");
        }
        qDebug("Success4");
    }else{
        qDebug("pi is NULL");
    }
}

